<?php

function backup_tables($host, $user, $pass, $name, $tables = '*') {

    $link = mysql_connect($host, $user, $pass);
    mysql_select_db($name, $link);

    //get all of the tables
    if ($tables == '*') {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while ($row = mysql_fetch_row($result)) {
            $tables[] = $row[0];
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }

    //cycle through
    foreach ($tables as $table) {
        $result = mysql_query('SELECT * FROM ' . $table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE ' . $table . ';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
        $return.= "\n\n" . $row2[1] . ";\n\n";

        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = mysql_fetch_row($result)) {
                $return.= 'INSERT INTO ' . $table . ' VALUES(';
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return.= '"' . $row[$j] . '"';
                    } else {
                        $return.= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return.= ',';
                    }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n### End of dump ###";
    }

    echo '<pre>';
    echo $return;
    echo '</pre>';
    //save file
//    $handle = fopen('db-backup-' . time() . '-' . (md5(implode(',', $tables))) . '.sql', 'w+');
//    fwrite($handle, $return);
//    fclose($handle);
}
?>
<div class="wrap">
    <div id="icon-options-general" class="icon32"><br></div>
    <h2>Panel instalacji</h2>

    <!--    <h3>Zmień logo</h3>-->
    <table class="form-table">
        <tbody>
            <tr valign="top">
                <th scope="row"><?php _e('Podaj subdomenę'); ?></th>
                <td>
                    <form enctype="multipart/form-data" id="upload-form" method="post" action="">
                        <input type="text" id="name" name="import" />

                        <input type="hidden" name="action" value="save" />
                        <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
                        <?php submit_button(__('Ok'), 'button', 'submit', false); ?>
                    </form>
                </td>
            </tr>
        </tbody>
    </table>

    <?php

    function install_abee_wp() {
        $pagename = 'testowanie';

        //getting server root folder for wordpress installation
        $abeepath = ABSPATH;
        $pathArray = explode('/', $abeepath);

        array_pop($pathArray);
        array_pop($pathArray);

        $serverRoot = implode('/', $pathArray);

        $installRoot = $serverRoot . '/' . $pagename;

        if (file_exists($installRoot)) {
            echo 'error: Katalog istnieje';
            return;
        } else {
            mkdir($installRoot);
            echo 'Katalog utworzony...';
        }

        //sprawdzanie i kopiowanie instalki:
        $abeewp = ABSPATH . 'wp-content/plugins/abeePro/wordpress.zip';
        if (!file_exists($abeewp)) {
            echo 'error: brak instalki';
            return;
        } else {
            copy($abeewp, $installRoot . '/wordpress.zip');
            echo 'Instalka skopiowana...';
        }

        $zip = new ZipArchive;
        if ($zip->open($installRoot . '/wordpress.zip') === TRUE) {
            $zip->extractTo($installRoot . '/');
            $zip->close();
            echo 'Wypakowanie plików...';
        } else {
            echo 'failed';
        }
    }

    global $wpdb;
//    backup_tables($wpdb->dbhost, $wpdb->dbuser, $wpdb->dbpassword, $wpdb->dbname);
    ?>



</div>
